package week10;

public class PP13_3 {
    public static void main(String[] args) {
        SelectionSort selectionSort = new SelectionSort();
        selectionSort.add(9);
        selectionSort.add(89);
        selectionSort.add(34);
        selectionSort.add(3);
        selectionSort.add(6);
        selectionSort.add(2);

        System.out.print(selectionSort);
        selectionSort.SelectionSortList();
        System.out.print(selectionSort);

    }
}

class SelectionSort{
    protected SelectionSortNode list;

    public SelectionSort(){
        list = null;
    }

    public void add(int num){
        SelectionSortNode node = new SelectionSortNode(num);
        SelectionSortNode current;
        if (list == null){
            list = node;
        }
        else {
            current = list;
            while (current.next!=null){
                current = current.next;
            }
                current.next = node;
        }
    }

    @Override
    public String toString() {
        String result ="";
        SelectionSortNode current = list;
        while (current!=null){
            result+=current.num + "\t";
            current=current.next;
        }
        return result + "\n";
    }

    public  void SelectionSortList(){
        int min;
        SelectionSortNode a = null;
        SelectionSortNode b = null;

        for (a=list;a!=null;a=a.next){
            for (b=a.next;b!=null;b=b.next){
                if (a.num>b.num){
                    min = b.num;
                    b.num = a.num;
                    a.num = min;
                }
            }
        }
    }
}

class SelectionSortNode{
    protected SelectionSortNode next;
    protected int num;

    public SelectionSortNode(int num){
        this.num = num;
        next = null;
    }
}


